﻿using Cinemachine;
using GameBasic;
using UnityEngine;

namespace VCity
{
    public class VCams: MonoSingleton<VCams>
    {
        public CinemachineVirtualCamera cityCam;
        public CinemachineVirtualCamera characterCam;

        // start settings
        [Header("Character Camera")]
        public float charDistance;
        public Vector3 charDamping;
        public float characterCamZoomSpeed = 1;

        //
        internal bool isFirstPersonCam;

        // char cam
        Cinemachine3rdPersonFollow CharacterCamBody => characterCam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
        public float CharacterCamDistance => CharacterCamBody == null ? 0 : CharacterCamBody.CameraDistance;

        private void OnValidate()
        {
            var body = CharacterCamBody;
            if (body != null)
            {
                charDistance = body.CameraDistance;
                charDamping = body.Damping;
            }
        }

        public float UpdateCharCamZoom(float zoomInput)
        {
            float dist = 0;

            var body = CharacterCamBody;
            if (body != null)
            {
                dist = Mathf.Clamp(body.CameraDistance + zoomInput * characterCamZoomSpeed * Time.deltaTime, 0, 3);
                body.CameraDistance = dist;
            }

            return dist;
        }

        public void SetCharCamZoom(float lerp)
        {
            var body = CharacterCamBody;
            if (body != null)
            {
                body.CameraDistance = Mathf.Lerp(0, charDistance, lerp);
                body.Damping = lerp == 0 ? Vector3.zero : charDamping;
            }
        }

        public void SetFirstPersonCam(bool firstPerson)
        {
            this.isFirstPersonCam = firstPerson;
            SetCharCamZoom(firstPerson ? 0 : 1);
            //var collider = characterCam.GetComponent<CinemachineCollider>();
            //collider.enabled = !firstPerson;
        }
    }
}